package com.linzx.markdown.window;

import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.linzx.markdown.entity.DataCenter;
import com.linzx.markdown.processer.Processer;
import com.linzx.markdown.processer.SourceNoteData;
import com.linzx.markdown.processer.SourceNoteDataImpl;
import com.linzx.markdown.processer.impl.ProcesserImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author avicomlin
 * @Description:
 * @date 2021/1/17
 */
public class NoteListWindow {
    private JTextField tfTopic;
    private JTable tbContext;
    private JButton create;
    private JButton clear;
    private JButton close;
    private JLabel title;
    private JPanel button;
    private JPanel contextPanel;

    public NoteListWindow(Project  project, ToolWindow toolWindow) {
        init();
        create.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String text = tfTopic.getText();
                if (text == null || "".equals(text.trim()) ){
                    MessageDialogBuilder.YesNo yesNo = MessageDialogBuilder.yesNo("操作结果", "文件名不能为空");
                    yesNo.show();
                    return;
                }
                VirtualFile virtualFile = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(), project, project.getBaseDir());
                if (virtualFile !=null){
                    String path = virtualFile.getPath();
                    String fullName = path + "/" + text + ".md";
                    SourceNoteData sourceNoteData = new SourceNoteDataImpl(fullName,text,DataCenter.LIST);
                    Processer process = new ProcesserImpl();
                    try {
                        process.processor(sourceNoteData);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

            }
        });
        clear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
DataCenter.clear();
            }
        });
        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                toolWindow.hide(null);
            }
        });
    }

    private void init() {
        tbContext.setModel(DataCenter.TABLE_MODEL);
        tbContext.setEnabled(true);
    }


    public JPanel getContextPanel() {
        return contextPanel;
    }
}
